﻿
document.observe("dom:loaded", loadMain);

function loadMain() {
    if (strPhotos) {
        showListIMG();
    }    
   

};
/************************************Code for slide show**********************/
var curIMGList = 0;
var t1;
var t2;
var arrTOut = [null, null, 0, ""];
var linkIMG = "http://www.nhommua.net/";
var temp, temp1;
var arrIFocus = $("div_IMGList").select('i');

var indexImgShow = -1;
var argThumbDealDetail;

function showListIMG() {
    if (strPhotos.dsPhoto.length > 0) {

    
        var strMLIMG = "";
        var iStart = curIMGList * 5;
        var IEnd = iStart + 5 > strPhotos.dsPhoto.length ? strPhotos.dsPhoto.length : iStart + 5;
        var j = 0;
        for (var i = iStart; i < IEnd; i++) {
            temp = '<li id="dListImg_' + i + '"><a name="" href="javascript:void(0)"><img width="80" height="49"  src="' + strPhotos.dsPhoto[i].link + '" onclick="javascript:selectImage(this, ' + j + ', ' + i + ');"></a><i></i></li>';
            temp1 = '<li id="dListImg_' + i + '"><a name="" href="javascript:void(0)"><img width="80" height="49"  src="' + strPhotos.dsPhoto[i].link + '" onclick="javascript:selectImage(this,' + j + ', ' + i + ');"></a><i></i></li>';
            //strMLIMG += i == iStart ? '<div class="listIMG imgListfirst" id="dListImg_' + i + '"><img width="91" height="55" src="' + linkIMG + strPhotos.dsPhoto[i].link + '"></img></div>' : '<div class="listIMG" id="dListImg_' + i + '"><img width="91" height="55" src="' + linkIMG + strPhotos.dsPhoto[i].link + '"></img></div>';
            strMLIMG += i == iStart ? temp : temp1;
            j++
        }j = 0;
        $("div_IMGList").innerHTML = strMLIMG;
        $("div_MainLIMG").style.display = "block";
        
        argThumbDealDetail = $("div_IMGList").select('i');
        changeThumb();
        
        clearTimeout(t1);
        clearTimeout(t2);
        clearTimeout(arrTOut[0]);
        clearTimeout(arrTOut[1]);
        arrTOut[2] = iStart;
        $("aimgIndexBig1").setOpacity(1);
        $("aimgIndexBig2").setOpacity(0);
        $("imgShow1").src = strPhotos.dsPhoto[iStart].link.replace('/small', '/large');
        if (strPhotos.dsPhoto[iStart].title != "") {
            //$("divTitleIMG").style.display = "block";
            $("strTitleIMG").innerHTML = strPhotos.dsPhoto[iStart].title;
        } else {
            $("divTitleIMG").style.display = "none";
            $("strTitleIMG").innerHTML = "";
        }
        /*edit here 30/11/2010*/
        // $("dFocusImgShow").style.left = 6 + "px";
        if (arrTOut[3] != "stop") { arrTOut[1] = setTimeout("timeOutShowIMG()", 4000); }
        $("div_MainLIMG").select('.listIMG').each(function(obj) {
            $(obj).observe('click', function(e) {
                clearTimeout(arrTOut[0]);
                clearTimeout(arrTOut[1]);
                arrTOut[2] = $(obj).id.split('_')[1];
                
                changeIMGShow($(obj).id.split('_')[1]);
                if (arrTOut[3] != "stop") {
                    arrTOut[1] = setTimeout("timeOutShowIMG()", 4000);
                }
            });
        });
        /*end*/
    } else {
        $("div_MainLIMG").style.display = "none";
    }
}

function timeOutShowIMG() {
    
    clearTimeout(arrTOut[0]);
    clearTimeout(arrTOut[1]);
    arrTOut[2] = parseInt(arrTOut[2]) + 1;
    
    if (arrTOut[2] > strPhotos.dsPhoto.length - 1) {
        curIMGList = 0;
        arrTOut[3] = "stop";
        showListIMG();
    }
    else {
        if (arrTOut[2] % 5 == 0) {
            curIMGList = arrTOut[2] / 5;
            showListIMG();
        }
        else {
            changeIMGShow(arrTOut[2]);
        }
        arrTOut[0] = setTimeout("timeOutShowIMG()", 4000);
    }
}
//edit 29/9/2011
function changeThumb() {
    indexImgShow = indexImgShow < 0 ? 0 : indexImgShow;
    for (var j = 0; j < argThumbDealDetail.length; j++) {
        $(argThumbDealDetail[j]).style.display = (j == indexImgShow) ? "block" : "none";
    }
    indexImgShow = (argThumbDealDetail.length - 1 <= indexImgShow) ? -1 : indexImgShow + 1;
    
}
function selectImage(e, index, iList) {
    $('imgShow1').src = e.src.replace('small', 'large');
    $('imgShow2').src = e.src.replace('small', 'large');
    $("aimgIndexBig1").setOpacity(1);
    $("aimgIndexBig2").setOpacity(0);
    clearTimeout(arrTOut[0]);
    clearTimeout(arrTOut[1]);
    arrTOut[2] = index;
    indexImgShow = index;
    changeThumb();

    if (curIMGList >= (Math.ceil(strPhotos.dsPhoto.length / 5) - 1) && indexImgShow == -1) {
        arrTOut = [null, null, 0, ""]; indexImgShow = 0; curIMGList = 0;
        arrTOut[1] = setTimeout("showListIMG()", 4000);
    }
    else if (Math.floor((iList + 1) / 5) > curIMGList) {
        curIMGList = Math.floor((iList + 1) / 5); indexImgShow = 0;
        arrTOut[1] = setTimeout("showListIMG()", 4000);
    }
    else {
        index += (curIMGList * 5 + 1);
        arrTOut[1] = setTimeout("changeIMGShow(" + index + ")", 4000);
    } 
}
//end edit

function changeIMGShow(e) {
    changeThumb();
    /*edit here 30/11/2010*/
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);
    var left = (arrTOut[2] % 5) > 1 ? ((arrTOut[2] % 5) * 96 + 4 + (arrTOut[2] % 5) - 1) : ((arrTOut[2] % 5) * 96 + 4);
    if ((version >= 5.5) && (version < 7)) { left = (arrTOut[2] % 5) > 1 ? ((arrTOut[2] % 5) * 94 + 6 + (arrTOut[2] % 5) - 1) : ((arrTOut[2] % 5) * 94 + 6); }
    /*end*/
    //$("dFocusImgShow").style.left = left + 'px';
    //alert(arrIFocus[e % 5].style.display);
    arrIFocus[e % 5].style.display = "block";
    $('imgShow2').src = strPhotos.dsPhoto[e].link.replace('/small', '/large');
    srcImgShow = strPhotos.dsPhoto[e].link.replace('/small', '/large');
    $("strTitleIMG").innerHTML = strPhotos.dsPhoto[e].title;
    clearTimeout(t1);
    clearTimeout(t2);
    $("aimgIndexBig1").setOpacity(1);
    $("aimgIndexBig2").setOpacity(0);
    $('aimgIndexBig2').style.display = "block";
    timeOutListIMG1();
    timeOutListIMG2();
}
/*end*/
function timeOutListIMG1() {
    var getOpa = $("aimgIndexBig1").getOpacity();
    var OpaCur = getOpa - 0.08008;
    $("aimgIndexBig1").setOpacity(OpaCur);
    if (OpaCur > 0) {
        t1 = setTimeout("timeOutListIMG1()", 90);
    }
    else {
        clearTimeout(t1);
        $("imgShow1").src = srcImgShow;
    }
};
function timeOutListIMG2() {
    var getOpa = $("aimgIndexBig2").getOpacity();
    var OpaCur = getOpa + 0.0808;
    if (OpaCur < 1) {
        $("aimgIndexBig2").setOpacity(OpaCur);
        t2 = setTimeout("timeOutListIMG2()", 90);
    }
    else {
        clearTimeout(t2);
    }
};

if (strPhotos) {
    if (strPhotos.dsPhoto.length > 5) {
        $("btnPreIMG").show();
        $("btnNextIMG").show();
    }
    else {
        $("btnPreIMG").hide();
        $("btnNextIMG").hide();
    }
    $('btnNextIMG').observe('click', function(e) {
        curIMGList = curIMGList + 1;
        if (curIMGList > Math.ceil(strPhotos.dsPhoto.length / 5) - 1) {
            curIMGList = Math.ceil(strPhotos.dsPhoto.length / 5) - 1;
            return;
        };
        indexImgShow = 0;
        showListIMG();
    });
    $('btnPreIMG').observe('click', function(e) {
        curIMGList = curIMGList - 1;
        if (curIMGList < 0) {
            curIMGList = 0;
            return;
        }
        indexImgShow = 0;
        showListIMG();
    });
}